import { defineStore } from 'pinia';
import {
  ShopUserParams,
  QueryParams,
  SingleUserParams,
  GetShopUserModel,
  GetSingleUserModel,
  AddShopUserParams,
  UpdateUserState,
  UpdateUserRole,
} from '/@/api/shopper/model/shopUserModel';
import { ShopCommonResult } from '/@/api/shopper/model/common';
import type { ErrorMessageMode } from '/#/axios';
import {
  shopUsersApi,
  getSingleUser,
  addShopUsers,
  updateUserState,
  deleteSingleUser,
  putSingleUser,
  putUserRole,
} from '/@/api/shopper/shopUsers';
import { store } from '/@/store';

export const useShopUserStore = defineStore({
  id: 'shop-user',
  state: () => ({
    query: {
      id: null,
      username: '',
    },
  }),
  getters: {
    getQuery(): { id: number | any; username: string } {
      return this.query;
    },
  },
  actions: {
    setQuery(value: QueryParams) {
      this.query = { ...value };
    },
    /**
     * @description: users
     */
    async users(
      params: ShopUserParams & { mode?: ErrorMessageMode },
    ): Promise<GetShopUserModel | null> {
      try {
        const { mode, ...shopUserParams } = params;
        const data = await shopUsersApi(shopUserParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: add user
     */
    async addUsers(
      params: AddShopUserParams & { mode?: ErrorMessageMode },
    ): Promise<ShopCommonResult | null> {
      try {
        const { mode, ...AddShopUserParams } = params;
        const data = await addShopUsers(AddShopUserParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: update user
     */
    async updateUser(
      params: UpdateUserState & { mode?: ErrorMessageMode },
    ): Promise<ShopCommonResult | null> {
      try {
        const { mode, ...UpdateUserState } = params;
        const data = await updateUserState(UpdateUserState, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: delete user
     */
    async deleteUser(
      params: SingleUserParams & { mode?: ErrorMessageMode },
    ): Promise<ShopCommonResult | null> {
      try {
        const { mode, ...DeleteUserParams } = params;
        const data = await deleteSingleUser(DeleteUserParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: query single user
     */
    async singleUser(params: {
      id: number;
      mode?: ErrorMessageMode;
    }): Promise<GetSingleUserModel | null> {
      try {
        const { id, mode } = params;
        const data = await getSingleUser(id, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: edit single user
     */
    async editSingleUser(
      params: SingleUserParams & { mode?: ErrorMessageMode },
    ): Promise<GetSingleUserModel | null> {
      try {
        const { mode, ...SingleUserParams } = params;
        const data = await putSingleUser(SingleUserParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: edit user role
     */
    async editUserRole(
      params: UpdateUserRole & { mode?: ErrorMessageMode },
    ): Promise<GetSingleUserModel | null> {
      try {
        const { mode, ...UpdateUserRole } = params;
        const data = await putUserRole(UpdateUserRole, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
  },
});

// Need to be used outside the setup
export function useUserStoreWithOut() {
  return useShopUserStore(store);
}
